package com.example.backend.Service;

import com.example.backend.Mapper.ProjectMapper;
import com.example.backend.Mapper.TeamMapper;
import com.example.backend.entity.Project;
import com.example.backend.entity.Team;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.type.ErrorType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.*;

@Service
public class ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TeamMapper teamMapper;

    //    新建项目
    public Integer createTeam(String teamName, String pjctName) {
        if (projectMapper.projectNameAndPjctNameNameForProject(teamName, pjctName) != null) return 1;
        projectMapper.createProject(teamName, pjctName);
        return 0;
    }

    //    团队项目列表
    public HashMap<String, Object> getProjects(String teamName) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("projects", projectMapper.getProjects(teamName));
            map.put("status", 0);
        } catch (Exception e) {
            System.out.println(e);
            map.put("status", -1);
        }
        return map;
    }

    //    删除项目
    public Integer delProject(String teamName, String pjctName) {
        if (projectMapper.projectNameAndPjctNameNameForProject(teamName, pjctName) == null) return -1;
        try {
            projectMapper.delPreoject(teamName, pjctName);
            return 0;
        } catch (Exception e) {
            return -1;
        }
    }

    //    查看删除项目
    public HashMap<String, Object> getDelProjects(String teamName) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("projects", projectMapper.getDelProjects(teamName));
            map.put("status", 0);
        } catch (Exception e) {
            System.out.println(e);
            map.put("status", -1);
        }
        return map;
    }

    //    恢复已删除项目
    public Integer recoverProject(String teamName, String pjctName) {
        if (projectMapper.projectNameAndPjctNameNameForProject(teamName, pjctName) == null) return -1;
        try {
            projectMapper.recoverProject(teamName, pjctName);
            return 0;
        } catch (Exception e) {
            return -1;
        }
    }

    //    彻底删除项目
    public Integer removeProject(String teamName, String pjctName) {
        if (projectMapper.projectNameAndPjctNameNameForProject(teamName, pjctName) == null) return -1;
        try {
            projectMapper.removeProject(teamName, pjctName);
            return 0;
        } catch (Exception e) {
            return -1;
        }
    }

    //    重命名项目
    public Integer renameProject(String teamName, String pjctName, String newName) {
        if (projectMapper.projectNameAndPjctNameNameForProject(teamName, pjctName) == null) return -1;
        if (projectMapper.projectNameAndPjctNameNameForProject(teamName, newName) != null) return 1;
        try {
            projectMapper.renamePreoject(teamName, pjctName, newName);
            return 0;
        } catch (Exception e) {
            return -1;
        }
    }

    //个人项目
    public HashMap<String, Object> oneForProjects(String email) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("project", projectMapper.oneForProjects(email));
        map.put("status", 0);
        return map;
    }

    //搜索个人项目
    public HashMap<String, Object> searchOnesProjects(String word, String email) {
        StringBuilder pattern = new StringBuilder(".*");
        for (char c : word.toCharArray()) {
            pattern.append(c).append(".*");
        }
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<Project> result = new ArrayList<>();
        List<Project> allProject = projectMapper.oneForProjects(email);
        for (Project tmp : allProject) {
            if (Pattern.matches(String.valueOf(pattern),tmp.getPjctName())) {
                result.add(tmp);
            }
        }
        map.put("status", 0);
        map.put("project", result);
        return map;
    }

    //搜索个人项目
    public HashMap<String, Object> searchTeamProjects(String word, String teamName) {
        StringBuilder pattern = new StringBuilder(".*");
        for (char c : word.toCharArray()) {
            pattern.append(c).append(".*");
        }
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<Project> result = new ArrayList<>();
        List<Project> allProject = projectMapper.getProjects(teamName);
        for (Project tmp : allProject) {
            if (Pattern.matches(String.valueOf(pattern),tmp.getPjctName())) {
                result.add(tmp);
            }
        }
        map.put("status", 0);
        map.put("project", result);
        return map;
    }
}

